"""
Problem 80: https://projecteuler.net/problem=80

Problem statement: For the first one hundred natural numbers, find the total of
the digital sums of the first one hundred decimal digits for all the irrational
square roots.
"""


# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/6/1
'''




from decimal import Decimal, getcontext
def sqrt2decimalspart(n: int, precision: int) -> str:
    '''
    >>> print(sqrt2decimalspart(2,13))  # sqrt(2) = 1.414213...
    4142135623730
    '''
    extraPrec = 3
    intlen = len(str(int(n**0.5)))
    getcontext().prec = precision + intlen + extraPrec

    return str(pow(Decimal(n), Decimal(1/2)))[intlen+1: precision+intlen+1]


def decimalsSum(n: int, precision: int) -> int:
    '''
    ??? print(decimalsSum(2,100))
    475
    '''
    sd = sqrt2decimalspart(n, precision)
    # print(sd,len(sd))
    return sum(map(int, sd))


def solution(limit: int = 100, precision: int = 100) -> int:

    nums = set(range(limit+1)) - {i*i for i in range(int(limit**0.5)+1)}
    print(nums)
    return sum(decimalsSum(n, precision) for n in nums)


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=False)

    print(solution())
    # 40727
